

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>argoverse.data_loading.synchronization_database &mdash; argoverse  documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> argoverse
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">argoverse</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>argoverse.data_loading.synchronization_database</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for argoverse.data_loading.synchronization_database</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python3</span>

<span class="c1"># &lt;Copyright 2019, Argo AI, LLC. Released under the MIT license.&gt;</span>

<span class="kn">import</span> <span class="nn">glob</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">pdb</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">cast</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">argoverse.utils.camera_stats</span> <span class="k">import</span> <span class="n">RING_CAMERA_LIST</span><span class="p">,</span> <span class="n">STEREO_CAMERA_LIST</span>
<span class="kn">from</span> <span class="nn">argoverse.utils.json_utils</span> <span class="k">import</span> <span class="n">read_json_file</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="get_timestamps_from_sensor_folder"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.synchronization_database.get_timestamps_from_sensor_folder">[docs]</a><span class="k">def</span> <span class="nf">get_timestamps_from_sensor_folder</span><span class="p">(</span><span class="n">sensor_folder_wildcard</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot; Timestamp always lies at end of filename</span>

<span class="sd">        Args:</span>
<span class="sd">            sensor_folder_wildcard: string to glob to find all filepaths for a particular</span>
<span class="sd">                        sensor files within a single log run</span>

<span class="sd">        Returns:</span>
<span class="sd">            Numpy array of integers, representing timestamps</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">path_generator</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">sensor_folder_wildcard</span><span class="p">)</span>
    <span class="n">path_generator</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="nb">int</span><span class="p">(</span><span class="n">jpg_fpath</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;_&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="k">for</span> <span class="n">jpg_fpath</span> <span class="ow">in</span> <span class="n">path_generator</span><span class="p">])</span></div>


<div class="viewcode-block" id="SynchronizationDB"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.synchronization_database.SynchronizationDB">[docs]</a><span class="k">class</span> <span class="nc">SynchronizationDB</span><span class="p">:</span>

    <span class="c1"># Camera is 30 Hz (once per 33.3 milliseconds)</span>
    <span class="c1"># LiDAR is 10 Hz</span>
    <span class="c1"># Max we are halfway between 33.3 milliseconds on either side</span>
    <span class="c1"># then convert milliseconds to nanoseconds</span>
    <span class="n">MAX_LIDAR_RING_CAM_TIMESTAMP_DIFF</span> <span class="o">=</span> <span class="mf">33.3</span> <span class="o">*</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1e9</span>
    <span class="c1"># Stereo Camera is 5 Hz (once per 200 milliseconds)</span>
    <span class="c1"># LiDAR is 10 Hz</span>
    <span class="c1"># Since Stereo is more sparse, we look for 200 millisecond on either side</span>
    <span class="c1"># then convert milliseconds to nanoseconds</span>
    <span class="n">MAX_LIDAR_STEREO_CAM_TIMESTAMP_DIFF</span> <span class="o">=</span> <span class="mi">200</span> <span class="o">*</span> <span class="mf">1.0</span> <span class="o">*</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1e9</span>

<div class="viewcode-block" id="SynchronizationDB.__init__"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.synchronization_database.SynchronizationDB.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset_dir</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">collect_single_log_id</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot; Build the SynchronizationDB.</span>
<span class="sd">            Note that the timestamps for each camera channel are not identical, but they are clustered together.</span>

<span class="sd">            Args:</span>
<span class="sd">                dataset_dir: path to dataset.</span>
<span class="sd">                collect_single_log_id: log id to process. (All if not set)</span>

<span class="sd">            Returns:</span>
<span class="sd">                None</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Building SynchronizationDB&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">collect_single_log_id</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">log_fpaths</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;</span><span class="si">{dataset_dir}</span><span class="s2">/*&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">log_fpaths</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span><span class="s2">&quot;</span><span class="si">{dataset_dir}</span><span class="s2">/</span><span class="si">{collect_single_log_id}</span><span class="s2">&quot;</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">per_log_pose_index</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">for</span> <span class="n">log_fpath</span> <span class="ow">in</span> <span class="n">log_fpaths</span><span class="p">:</span>
            <span class="n">log_id</span> <span class="o">=</span> <span class="n">log_fpath</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">per_log_pose_index</span><span class="p">[</span><span class="n">log_id</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">camera_name</span> <span class="ow">in</span> <span class="n">STEREO_CAMERA_LIST</span> <span class="o">+</span> <span class="n">RING_CAMERA_LIST</span><span class="p">:</span>

                <span class="n">sensor_folder_wildcard</span> <span class="o">=</span> <span class="n">f</span><span class="s2">&quot;</span><span class="si">{dataset_dir}</span><span class="s2">/</span><span class="si">{log_id}</span><span class="s2">/</span><span class="si">{camera_name}</span><span class="s2">/</span><span class="si">{camera_name}</span><span class="s2">_*.jpg&quot;</span>
                <span class="n">tovs</span> <span class="o">=</span> <span class="n">get_timestamps_from_sensor_folder</span><span class="p">(</span><span class="n">sensor_folder_wildcard</span><span class="p">)</span>

                <span class="bp">self</span><span class="o">.</span><span class="n">per_log_pose_index</span><span class="p">[</span><span class="n">log_id</span><span class="p">][</span><span class="n">camera_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">tovs</span></div>

<div class="viewcode-block" id="SynchronizationDB.get_valid_logs"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.synchronization_database.SynchronizationDB.get_valid_logs">[docs]</a>    <span class="k">def</span> <span class="nf">get_valid_logs</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Iterable</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot; Return the log_ids for which the SynchronizationDatabase contains pose information.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">per_log_pose_index</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span></div>

<div class="viewcode-block" id="SynchronizationDB.get_closest_cam_channel_timestamp"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.synchronization_database.SynchronizationDB.get_closest_cam_channel_timestamp">[docs]</a>    <span class="k">def</span> <span class="nf">get_closest_cam_channel_timestamp</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lidar_timestamp</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">camera_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">log_id</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot; Grab the LiDAR pose file. Get its timestamp, and then find the pose message</span>
<span class="sd">            with the closest absolute timestamp.</span>

<span class="sd">            Instead of sorting the whole array of timestamp differences, we just</span>
<span class="sd">            take the minimum value (to speed up this function).</span>

<span class="sd">            Args:</span>
<span class="sd">                lidar_timestamp: integer</span>
<span class="sd">                camera_name: string, representing path to log directories</span>
<span class="sd">                log_id: string</span>

<span class="sd">            Returns:</span>
<span class="sd">                closest_cam_ch_timestamp: closest timestamp</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">log_id</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">per_log_pose_index</span> <span class="ow">or</span> <span class="n">camera_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">per_log_pose_index</span><span class="p">[</span><span class="n">log_id</span><span class="p">]:</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="n">timestamps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">per_log_pose_index</span><span class="p">[</span><span class="n">log_id</span><span class="p">][</span><span class="n">camera_name</span><span class="p">]</span>
        <span class="c1"># catch case if no files were loaded for a particular sensor</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">timestamps</span><span class="o">.</span><span class="n">tolist</span><span class="p">():</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="n">closest_ind</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">absolute</span><span class="p">(</span><span class="n">timestamps</span> <span class="o">-</span> <span class="n">lidar_timestamp</span><span class="p">))</span>
        <span class="n">closest_cam_ch_timestamp</span> <span class="o">=</span> <span class="n">cast</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">timestamps</span><span class="p">[</span><span class="n">closest_ind</span><span class="p">])</span>  <span class="c1"># mypy does not understand numpy arrays</span>
        <span class="n">timestamp_diff</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">absolute</span><span class="p">(</span><span class="n">lidar_timestamp</span> <span class="o">-</span> <span class="n">closest_cam_ch_timestamp</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">timestamp_diff</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">MAX_LIDAR_RING_CAM_TIMESTAMP_DIFF</span> <span class="ow">and</span> <span class="n">camera_name</span> <span class="ow">in</span> <span class="n">RING_CAMERA_LIST</span><span class="p">:</span>
            <span class="c1"># convert to nanoseconds-&gt;milliseconds for readability</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="s2">&quot;No corresponding image: </span><span class="si">%s</span><span class="s2"> &gt; </span><span class="si">%s</span><span class="s2"> ms&quot;</span><span class="p">,</span> <span class="n">timestamp_diff</span> <span class="o">/</span> <span class="mf">1e6</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">MAX_LIDAR_RING_CAM_TIMESTAMP_DIFF</span> <span class="o">/</span> <span class="mf">1e6</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">elif</span> <span class="n">timestamp_diff</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">MAX_LIDAR_STEREO_CAM_TIMESTAMP_DIFF</span> <span class="ow">and</span> <span class="n">camera_name</span> <span class="ow">in</span> <span class="n">STEREO_CAMERA_LIST</span><span class="p">:</span>
            <span class="c1"># convert to nanoseconds-&gt;milliseconds for readability</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="s2">&quot;No corresponding image: </span><span class="si">%s</span><span class="s2"> &gt; </span><span class="si">%s</span><span class="s2"> ms&quot;</span><span class="p">,</span>
                <span class="n">timestamp_diff</span> <span class="o">/</span> <span class="mf">1e6</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">MAX_LIDAR_STEREO_CAM_TIMESTAMP_DIFF</span> <span class="o">/</span> <span class="mf">1e6</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">closest_cam_ch_timestamp</span></div></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="c1"># example usage</span>

    <span class="n">root_dir</span> <span class="o">=</span> <span class="s2">&quot;./&quot;</span>
    <span class="n">camera</span> <span class="o">=</span> <span class="s2">&quot;ring_front_center&quot;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="s2">&quot;c6911883-1843-3727-8eaa-41dc8cda8993&quot;</span>

    <span class="n">db</span> <span class="o">=</span> <span class="n">SynchronizationDB</span><span class="p">(</span><span class="n">root_dir</span><span class="p">)</span>
    <span class="n">get_timestamps_from_sensor_folder</span><span class="p">(</span><span class="n">root_dir</span><span class="p">)</span>
    <span class="n">db</span><span class="o">.</span><span class="n">get_valid_logs</span><span class="p">()</span>
    <span class="n">db</span><span class="o">.</span><span class="n">get_closest_cam_channel_timestamp</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">camera</span><span class="p">,</span> <span class="nb">id</span><span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Argo AI, LLC

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>